package srdb.migration.common;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;

public abstract class MTKConnection
        implements IMTKConnection {
    protected Connection conn = null;
    protected String dbURL = null;
    protected String dbUser = null;
    protected String dbPassword = null;

    protected Hashtable dbServerParams = null;

    public MTKConnection(String dbURL, String dbUser, String dbPassword)
            throws Exception {
        this.dbURL = dbURL;
        this.dbUser = dbUser;
        this.dbPassword = dbPassword;
    }

    public MTKConnection(String PropertiesFile) throws Exception {
    }
    public MTKConnection(){
    }

    public Connection getConnection() {
        return this.conn;
    }

    public void setTransactionIsolationLevel(int transactionIsolationLevel) throws SQLException {
        this.conn.setTransactionIsolation(transactionIsolationLevel);
    }

    public void setAutoCommit(boolean autoCommit) throws SQLException {
        this.conn.setAutoCommit(autoCommit);
    }

    public boolean getAutoCommit() throws SQLException {
        return this.conn.getAutoCommit();
    }

    public void commit()
            throws SQLException {
        if (!this.conn.getAutoCommit())
            this.conn.commit();
    }

    public void rollback()
            throws SQLException {
        if (!this.conn.getAutoCommit())
            this.conn.rollback();
    }

    public String getDBServerParamValue(String paramName) throws SQLException {
        if (this.dbServerParams == null) {
            return null;
        }

        Enumeration params = this.dbServerParams.keys();
        while (params.hasMoreElements()) {
            String param = (String) params.nextElement();
            if (param.equalsIgnoreCase(paramName)) {
                return (String) this.dbServerParams.get(param);
            }
        }

        return null;
    }

}
